package io.gitee.zhangbinhub.admin.controller.api

import cn.dev33.satoken.annotation.SaCheckPermission
import cn.dev33.satoken.stp.StpUtil
import io.gitee.zhangbinhub.acp.boot.exceptions.WebException
import io.gitee.zhangbinhub.acp.boot.log.LogAdapter
import io.gitee.zhangbinhub.acp.core.common.CommonTools
import io.gitee.zhangbinhub.admin.api.ServerApi
import io.gitee.zhangbinhub.admin.base.BaseController
import io.gitee.zhangbinhub.admin.constant.ModuleFuncCode
import io.gitee.zhangbinhub.admin.constant.RoleCode
import io.gitee.zhangbinhub.admin.entity.Organization
import io.gitee.zhangbinhub.admin.po.OrganizationPo
import io.gitee.zhangbinhub.admin.service.OrganizationService
import io.gitee.zhangbinhub.admin.vo.InfoVo
import io.gitee.zhangbinhub.admin.vo.OrganizationVo
import io.swagger.annotations.Api
import io.swagger.annotations.ApiOperation
import io.swagger.annotations.ApiParam
import org.noear.solon.annotation.*
import org.noear.solon.core.handle.MethodType
import org.noear.solon.core.util.MimeType
import org.noear.solon.validation.annotation.*

@Valid
@Controller
@Api("机构信息")
open class OrgController(
    @Inject logAdapter: LogAdapter,
    private val organizationService: OrganizationService
) : BaseController(logAdapter) {

    @ApiOperation("获取机构列表", notes = "查询所有机构列表")
    @Mapping(
        method = [MethodType.GET],
        value = ServerApi.basePath + ServerApi.orgConfig,
        produces = MimeType.APPLICATION_JSON_VALUE
    )
    fun orgList(): List<Organization> = organizationService.getOrgList()

    @ApiOperation("获取可编辑的机构列表", notes = "查询所有可编辑的机构列表")
    @Mapping(
        method = [MethodType.GET],
        value = ServerApi.basePath + ServerApi.modifiableOrg,
        produces = MimeType.APPLICATION_JSON_VALUE
    )
    @Throws(WebException::class)
    fun modOrgList(): List<Organization> = organizationService.getModOrgList(StpUtil.getLoginIdAsString())

    @ApiOperation("获取所属机构及其所有子机构列表（所属机构）")
    @Mapping(
        method = [MethodType.GET],
        value = ServerApi.basePath + ServerApi.currAndAllChildrenOrg,
        produces = MimeType.APPLICATION_JSON_VALUE
    )
    @Throws(WebException::class)
    fun currAndAllChildrenOrgList(): List<Organization> =
        organizationService.getCurrAndAllChildrenForOrg(StpUtil.getLoginIdAsString())

    @ApiOperation("获取所属机构及其所有子机构列表（管理机构）")
    @Mapping(
        method = [MethodType.GET],
        value = ServerApi.basePath + ServerApi.currAndAllChildrenMngOrg,
        produces = MimeType.APPLICATION_JSON_VALUE
    )
    @Throws(WebException::class)
    fun currAndAllChildrenForMngOrg(): List<Organization> =
        organizationService.getCurrAndAllChildrenForMngOrg(StpUtil.getLoginIdAsString())

    @ApiOperation("获取所属机构及其所有子机构列表（所有机构）")
    @Mapping(
        method = [MethodType.GET],
        value = ServerApi.basePath + ServerApi.currAndAllChildrenAllOrg,
        produces = MimeType.APPLICATION_JSON_VALUE
    )
    @Throws(WebException::class)
    fun currAndAllChildrenForAllOrg(): List<Organization> =
        organizationService.getCurrAndAllChildrenForAllOrg(StpUtil.getLoginIdAsString())

    @ApiOperation("新建机构信息", notes = "名称、编码、上级ID、序号、关联用户")
    @SaCheckPermission(ModuleFuncCode.orgAdd, orRole = [RoleCode.SUPER])
    @Mapping(
        method = [MethodType.PUT],
        value = ServerApi.basePath + ServerApi.orgConfig,
        produces = MimeType.APPLICATION_JSON_VALUE
    )
    @Throws(WebException::class)
    fun add(@Body @Validated organizationPo: OrganizationPo): Organization =
        organizationService.doCreate(StpUtil.getLoginIdAsString(), organizationPo)

    @ApiOperation("删除指定的机构信息")
    @SaCheckPermission(ModuleFuncCode.orgDelete, orRole = [RoleCode.SUPER])
    @Mapping(
        method = [MethodType.DELETE],
        value = ServerApi.basePath + ServerApi.orgConfig,
        produces = MimeType.APPLICATION_JSON_VALUE
    )
    @Throws(WebException::class)
    fun delete(
        @ApiParam("id列表", required = true)
        @NotEmpty(message = "id不能为空")
        @NotNull(message = "id不能为空")
        @Body
        idList: MutableList<String>
    ): InfoVo = organizationService.doDelete(StpUtil.getLoginIdAsString(), idList).let { InfoVo(message = "删除成功") }

    @ApiOperation("更新机构信息", notes = "名称、编码、上级ID、序号、关联用户")
    @SaCheckPermission(ModuleFuncCode.orgUpdate, orRole = [RoleCode.SUPER])
    @Mapping(
        method = [MethodType.PATCH],
        value = ServerApi.basePath + ServerApi.orgConfig,
        produces = MimeType.APPLICATION_JSON_VALUE
    )
    @Throws(WebException::class)
    fun update(@Body @Validated organizationPo: OrganizationPo): Organization {
        if (CommonTools.isNullStr(organizationPo.id)) {
            throw WebException("ID不能为空")
        }
        return organizationService.doUpdate(StpUtil.getLoginIdAsString(), organizationPo)
    }

    @ApiOperation("获取机构详细信息")
    @SaCheckPermission(ModuleFuncCode.orgQuery, orRole = [RoleCode.SUPER])
    @Mapping(
        method = [MethodType.GET],
        value = ServerApi.basePath + ServerApi.orgConfig + "/{orgId}",
        produces = MimeType.APPLICATION_JSON_VALUE
    )
    @Throws(WebException::class)
    fun orgInfo(
        @ApiParam("机构id", required = true)
        @NotBlank(message = "机构id不能为空")
        @Path(name = "orgId")
        orgId: String
    ): OrganizationVo = organizationService.getOrgInfo(orgId)

    @ApiOperation("获取机构列表（编码或名称）")
    @Mapping(
        method = [MethodType.GET],
        value = ServerApi.basePath + ServerApi.orgConfig + "-by-code-or-name",
        produces = MimeType.APPLICATION_JSON_VALUE
    )
    @Throws(WebException::class)
    fun orgInfoByCodeOrName(
        @ApiParam("机构编码或名称", required = true)
        @NotBlank(message = "机构编码或名称不能为空")
        @Param(name = "codeOrName")
        codeOrName: String
    ): MutableList<OrganizationVo> =
        organizationService.getOrgInfoByCodeOrName(codeOrName)
}